/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = ora_resultset_accessor.c                           */
/*  NOTE      =                                                    */
/*  DATE      = 2015/01/14 by songbb                               */
/*******************************************************************/

#include <frlog.h>
#include "frcomm.h"

#include "conntrack.h"
#include "frct_comm.h"

#include "../../ora_core.h"
#include "../../comm/ora_comm.h"
#include "../../comm/ora_util.h"
#include "../ora_mar_engine.h"
#include "ora_resultset_accessor.h"
#include "../ttc8/ora_ttc8_tti_dcb.h"
#include "../../comm/ora_stmt.h"

/*******************************************************************************
*  FUNC     :  解析 resultset 类型数据
*  ARGS     :  *p_st_conn                           (ct信息)
*           :  *p_st_sess                           (Oracle私有的session)
*           :  *p_st_ora_packet                     (Oracle数据包)
*           :  *p_st_metadata                       (meta信息)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int ora_resultset_accessor_unmarshal_one_row(Conntrack *p_st_conn
                                            , L7session_ora *p_st_sess
                                            , L7_ora_packet *p_st_ora_packet
                                            , Sql_metadata_info *p_st_metadata)
{
    L7_session *p_st_session = frct_get_session(p_st_conn);
    L7session_ora_private *p_st_session_private = (L7session_ora_private *) frct_get_session_private(p_st_session);
    Sql_statement *p_st_stmt = NULL;
    short s_tmp = 0;
    long long ll_tmp = 0LL;
    long long ll_cursor = 0LL;

    if (p_st_metadata->i_isuseless == FR_YES || p_st_metadata->i_isnullbydescribe == FR_YES)
        goto success;

    if (ora_mar_unmarshalUB1(p_st_ora_packet, &s_tmp) != FR_SUCC) // i in receiveFromRefCursor
        goto error;
    if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_tmp) != FR_SUCC) // j in receiveFromRefCursor
        goto error;
    if (ll_tmp != 0LL)
    {
        p_st_session_private->p_st_curr_sess->ul_cursor = 0L;
        // 新建一个游标为0的 Sql_statement
        if (ora_create_new_stmt(p_st_conn, &p_st_stmt) != FR_SUCC)
            goto error;
        if (ora_ttc8_tti_dcb_receiveCommon(p_st_conn, p_st_sess, p_st_ora_packet, FR_NO, p_st_stmt) != FR_SUCC)
            goto error;
        if (ora_mar_unmarshalUB4(p_st_sess, p_st_ora_packet, &ll_cursor) != FR_SUCC) // cursor
            goto error;
        if (ll_cursor > 0LL)
        {
            // 将游标0更新为从服务端返回来的游标值
            if (frdb_add_statement(p_st_session_private->p_hash_stmt, (long) ll_cursor, p_st_stmt) != FR_SUCC)
                goto error;
            ORA_PRINT_DBG("add stmt of cursor 0 to hash_stmt and update to cursor:[%lld]\n", ll_cursor);
        }
    }

success:
    return FR_SUCC;
error:
    frdb_statement_free(&p_st_stmt);
    return FR_FAIL;
}
